import {
  formatParseJson
} from './util'
// import axios from 'axios'
/**
 * ES6写法的JSBridge
 *
 * @export
 * @class JSBridge
 */

export default class JSBridge {
  constructor () {
    this.name = 'JSBridge'
    this.reset = true
    this.instance = null
    this.userAgent = 'h5'
  }

  static getInstance () {
    if (!this.instance) {
      this.instance = new JSBridge()
    }
    return this.instance
  }

  /**
   * 判断设备类型
   *
   * @returns
   * @memberof JSBridge
   */
  filterRoute () {
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    // eslint-disable-next-line
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)

    if (process.env.NODE_ENV === 'production') {
      if (isAndroid) {
        this.userAgent = 'android'
      } else if (isiOS) {
        this.userAgent = 'ios'
      } else {
        this.userAgent = 'h5'
      }
    }

    return isAndroid
  }

  /**
   * 判断设备类型
   *
   * @returns
   * @memberof JSBridge
   */
  device () {
    var u = navigator.userAgent
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    // eslint-disable-next-line
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)

    if (process.env.NODE_ENV === 'production') {
      if (isAndroid) {
        this.userAgent = 'android'
      } else if (isiOS) {
        this.userAgent = 'ios'
      } else {
        this.userAgent = 'h5'
      }
    }

    return isAndroid
  }

  /**
   * JSBridge初始化
   *
   * @param {*} callback
   * @returns
   * @memberof JSBridge
   */
  _init (callback) {

  }

  /**
   * 从原生获取登录令牌
   * @param {*} callback
   */
  getAccessToken (callback) {
    // axios
    //   .get('/authorize')
    //   .then(response => {
    //     let data = response.data
    //     console.log(data)
    //   })
    //   .catch(function (error) {
    //     console.log(error)
    //   })
    let data = {
      pixelRatio: 2,
      screenWidth: 375,
      screenHeight: 667,
      statusBarHeight: 20,
      navigationBarHeight: 64,
      build: 3,
      platform: 'ios'
    }
    setTimeout(function () {
      callback(data)
    }, process.env.LOADING_TIME)
  }

  /**
   * 获取系统信息
   * @param {*} callback
   */
  getSystemInfo (callback) {
    if (process.env.NODE_ENV === 'development') {
      let data = {
        pixelRatio: 2,
        screenWidth: 375,
        screenHeight: 667,
        statusBarHeight: 20,
        navigationBarHeight: 64,
        build: 3,
        platform: 'ios'
      }
      setTimeout(function () {
        callback(data)
      }, process.env.LOADING_TIME)
    } else {
      this._init(function (bridge) {
        this.__init__(bridge)
        bridge.callHandler('getSystemInfo', {}, function (response) {
          callback(formatParseJson(response))
        })
      }.bind(this))
    }
  }

  /**
   * 跳转到登录界面
   * @param {*} data
   * @param {*} callback
   */
  openLoginPage (callback) {
    console.log('跳转到登录界面')
  }

  /**
   * 授权页面
   * @param {*} data
   * @param {*} callback
   */
  openAuthentPage (callback) {
    console.log('跳转到授权页面')
  }
}
